package easy

import (
	. "GoLeetcode/common"
)

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */

// 当一个节点的左子节点是叶子节点时，才把值相加
func sumOfLeftLeaves(root *TreeNode) int {
	var dfs func(node *TreeNode) int

	// 判断节点是否是叶子节点
	var isLeafNode func(node *TreeNode) bool
	isLeafNode = func(node *TreeNode) bool {
		return node.Left == nil && node.Right == nil
	}

	// 1.处理当前节点
	// 2.递归处理子节点
	dfs = func(node *TreeNode) int {
		if node == nil {
			return 0
		}
		var result int
		if node.Left != nil {
			if isLeafNode(node.Left) {
				result += node.Left.Val
			} else {
				result += dfs(node.Left)
			}
		}

		result += dfs(node.Right)

		return result
	}
	return dfs(root)
}
